import type { RegistrableApp } from 'qiankun'
import router from '@/router'

// 微应用配置列表
export const microApps: RegistrableApp<any>[] = [
  {
    name: 'subapp',
    entry: process.env.NODE_ENV === 'development' ? 'http://localhost:8090' : '/subapp/entry.js',
    container: '#subapp-container',
    activeRule: '/subapp',
    props: {
      // 可以传递的自定义props
      mainRouter: router,
    },
  },
  {
    name: 'Investment',
    entry:
      process.env.NODE_ENV === 'development' ? 'http://localhost:8091' : '/Investment/entry.js',
    container: '#Investment-container',
    activeRule: '/Investment',
    props: {
      mainRouter: router,
      // 可以传递的自定义props
    },
  },
  {
    name: 'jssub',
    entry: process.env.NODE_ENV === 'development' ? 'http://localhost:8092' : '/jssub/entry.js',
    container: '#jssub-container',
    activeRule: '/jssub',
    props: {
      mainRouter: router,
      // 可以传递的自定义props
    },
  },
  {
    name: 'Mixingplant',
    entry:
      process.env.NODE_ENV === 'development' ? 'http://localhost:8093' : '/Mixingplant/entry.js',
    container: '#Mixingplant-container',
    activeRule: '/Mixingplant',
    props: {
      mainRouter: router,
      // 可以传递的自定义props
    },
  },
]

// 微应用生命周期配置
export const microAppsLifeCycles = {
  beforeLoad: [
    async (app: any) => {
      console.log('[主应用] 开始加载:', app.name)
    },
  ],
  beforeMount: [
    async (app: any) => {
      console.log('[主应用] 准备挂载:', app.name)
    },
  ],
  afterMount: [
    async (app: any) => {
      console.log('[主应用] 挂载完成:', app.name)
    },
  ],
  afterUnmount: [
    async (app: any) => {
      console.log('[主应用] 已卸载:', app.name)

      //
      // 卸载时移除所有的子框架相关样式
      // window.document.head.querySelectorAll('style[data-vite-dev-id]').forEach((el) => {
      //   if (el.getAttribute('data-vite-dev-id')?.includes('Rim.WebMS.' + app.name)) {
      //     el.remove()
      //   }
      // })
    },
  ],
}

// 导出获取微应用配置的方法
export const getMicroAppConfig = (appName: string) => {
  return microApps.find((app) => app.name === appName)
}
